﻿using AiMei.Redis.Core;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace AiMei.Redis.Wrapper
{
    internal class RedisDataBase:IDatabase
    {

        private RedisConnect redisConnect;
        public RedisDataBase(RedisConnect redisConnect)
        {
            this.redisConnect = redisConnect;
        }
        public string StringGet(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("参数key不能为空字符串");
            }

            var comm = $"get {key}".CreateRedisCommon();
            redisConnect.clientChannel.WriteAndFlushAsync(comm);

            TaskCompletionSource<RedisObject> tcs = new TaskCompletionSource<RedisObject>();

            Utils.Enqueue(tcs);

            //Console.WriteLine("长度:{0}", Utils.TaskQueue.Count);
            try
            {
                tcs.Task.Wait(5 * 1000);
                var result = tcs.Task.Result;
                if (result is SimpleString)
                    return result.ToString();
                else
                {
                    throw new Exception(result.ToString());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
